package com.study.ssm.aop.proxy.jdk;

import com.study.ssm.aop.proxy.UserService;
import com.study.ssm.aop.proxy.UserServiceImpl;
import com.study.ssm.factory.bean.User;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zhoujunlin
 * @date 2023年07月24日 9:59
 * @desc
 */
public class UserServiceJDKProxy {

    public static void main(String[] args) {
        // 原始对象
        UserService userService = new UserServiceImpl();

        InvocationHandler invocationHandler = new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("==================before log===============");
                return method.invoke(userService, args);
            }
        };

        UserService userServiceProxy = (UserService) Proxy.newProxyInstance(UserServiceJDKProxy.class.getClassLoader(),
                userService.getClass().getInterfaces(), invocationHandler);

        userServiceProxy.register(new User());
        userServiceProxy.query("zhoujl", "123456");

    }

}
